const utils = require('../../../../common/util/util')

Page({
  data: {
    nav: [
      {
        title: '最新',
        key: ''
      },
      {
        title: '热门',
        key: ''
      }
    ],
    active: 0,
    info: '',
    label: '',
    id: '',
    readyMsg: 0,
    loading: true,
    page: 1,
    warning: true,
    list: [],
    isFixedTop: false,
    navbarInitTop: 0,
    statusBar: '',
    customBar: '',
    grouplist:[],
    state:'navbar',
    openid:''
  },
  onShareAppMessage: function () {
    // return {
    //   title: this.data.label
    // };
  },
  onLoad: function () {
    let that = this
    this.setData({
      page:1
    })
    this.xInit()
    // this.xGetPostsInfo()
    this.xReady()
  },
  onShow: function () {
    this.xTogetuser();
    this.xScroll()
    this.xSystem()
    this.xgetgroup()
  },
  onReachBottom: function (e) {
    let that = this
    that.setData({
      warning: false,
    })
    that.xInit()
  },
  onPageScroll: function (e) {
    let that = this;
    var scrollTop = parseInt(e.scrollTop);
    var isSatisfy = scrollTop >= that.data.navbarInitTop ? true : false;
    if(that.data.state=='navbar'){
      if (that.data.isFixedTop === isSatisfy) {
        return false;
      }
      that.setData({
        isFixedTop: isSatisfy
      });
    }else{
      if(scrollTop==0){
        that.setData({
          isFixedTop: true,
          state:'navbar'
        });
      }else{
        that.setData({
          isFixedTop: true
        });
      }
    }
  },
  onUnload: function () {
    console.log('触发刷新 => Start')
    // this.xChildReload()
  },
  xTolist : function(){
    wx.navigateTo({
      url: '../list/index',
    })
  },
  xTogetuser :function(){
    let that = this;
    wx.cloud.callFunction({
      name: 'user',
      data: {
          action: 'detail'
      }
    }).then(res => {
        let user = res.result
        that.setData({
            openid: user.user_id
        })
    })
  },
  xChildReload: function () {
    var pages = getCurrentPages();//当前页面栈
    // if (pages.length > 1) {
    //   var beforePage = pages[pages.length - 2];//获取上一个页面实例对象
    //   beforePage.xParReload();//触发父页面中的方法
    // }
  },

  xScroll: function () {
    let that = this;
    if (that.data.navbarInitTop == 0) {
      wx.createSelectorQuery().select('#navbar').boundingClientRect(function (rect) {
        if (rect && rect.top > 0) {
          var navbarInitTop = parseInt(rect.top);
          console.log(navbarInitTop)
          that.setData({
            navbarInitTop: navbarInitTop - 40
          });
        }
      }).exec();
    }
  },

  xSystem: function () {
    let that = this
    wx.getSystemInfo({
      success: (e) => {
        let custom = wx.getMenuButtonBoundingClientRect();
        that.setData({
          statusBar: e.statusBarHeight,
          customBar: custom.bottom + custom.top - e.statusBarHeight
        })
      }
    })
  },

  xGetPostsInfo: function (id) {
    let that = this
    wx.cloud.callFunction({
      name: 'dynamic_content',
      data: {
        action: "get_count_by_label",
        label_id: id,
      }
    }).then(res => {
      let data = res.result
      console.log(res)
      that.setData({
        info: data[0]
      })
    })
  },

  // xParReload: async function () {
  //   console.log('触发刷新 => end')
  //   wx.showLoading({
  //     title: '加载中',
  //   })
  //   let that = this
  //   let active = that.data.active
  //   let page = that.data.page
  //   let list = []
  //   for (var i = 0; i < page; i++) {
  //     await wx.cloud.callFunction({
  //       name: 'dynamic_content',
  //       data: {
  //         action: "get_dynamic_content_list",
  //         page_number: page,
  //         type: active + 1
  //       }
  //     }).then(res => {
  //       let data = res.result
  //       for (var k in data) {
  //         data[k].release_time = utils.formatTime_emotion(new Date(data[k].release_time))
  //         list.push(data[k])
  //       }
  //       that.setData({
  //         list: list
  //       })
  //       wx.hideLoading()
  //     })
  //   }
  // },
  xgetgroup: function(){
    let that=this;
    wx.cloud.callFunction({
      name: 'dynamic_content',
      data: {
          action: "get_group_list_by_follow"
         }
      }).then(res => {
         if(res.result.length>0){
            that.setData({
              grouplist:res.result
            })
         }else{
            that.getdefault()
         }
      })
   
  },
  getdefault :function(){
    wx.cloud.callFunction({
      name: 'dynamic_content',
      data: {
        action: "get_group_recommendation"
      }
    }).then(res => {
        this.setData({
          grouplist:res.result
        })
    })
  },
  xToIndex: function (e) {
    let id = e.currentTarget.dataset.id
    let label = e.currentTarget.dataset.label
    let mylike = e.currentTarget.dataset.mylike
    wx.navigateTo({
        url: '/subtool/pages/community/about/index?id=' + id + '&label=' + label + '&mylike=' + mylike,
    })
  },
  xInit: function () {
    let that = this
    let active = that.data.active
    let page = that.data.page
    let list = that.data.list
    wx.cloud.callFunction({
      name: 'dynamic_content',
      data: {
        action: "get_dynamic_content_list",
        page_number: page,
        type: active + 1
      }
    }).then(res => {
      console.log("返回标签下的内容:", res.result)
      let data = res.result
      for (var i in data) {
        data[i].release_time = utils.formatTime_emotion(new Date(data[i].release_time))
        list.push(data[i])
      }
      if (data.length === 0) {
        that.setData({
          list: list,
          page: page,
          loading: false,
          warning: true
        })
        setTimeout(() => {
          that.setData({
            warning: true
          })
        }, 2000)
      } else {
        page += 1
        that.setData({
          list: list,
          page: page,
          loading: false,
          page: page,
        })
      }
      wx.hideLoading()
    })
  },

  xNavSelect: function (e) {
    wx.showLoading({
      title: '加载中',
    })
    let that = this
    let id = that.data.id
    let key = e.currentTarget.dataset.key
    that.setData({
      active: key,
      page: 1,
      list: [],
      loading: true,
      state:e.currentTarget.dataset.state
    })
    that.xInit(id)
  },

  xToCreate: function () {
    let that = this
    let id = that.data.id
    let label = that.data.label
    wx.navigateTo({
        url: '/subtool/pages/community/index/index?id=' + id + '&label=' + label,
    })
  },
  //  日记练习
  xTopractice: function(e){
    let id = e.currentTarget.dataset.id
    let name = e.currentTarget.dataset.name
    let num = e.currentTarget.dataset.num
    wx.navigateTo({
      url: '/subtool/pages/dayremb/detdayremb/index?id='+id+'&title=' +name +'&num='+num,
    })
  },
  xLike: function (e) {
    wx.showLoading({
      title: '刷新中',
    })
    let that = this
    let id = that.data.id
    let params = e.currentTarget.dataset
    wx.cloud.callFunction({
      name: 'dynamic_content',
      data: {
        action: "thumbs_up",
        _id: params.id,
      }
    }).then(res => {
      that.setData({
        [`list[${params.index}].me_thumbs_up`]: !params.state,
        [`list[${params.index}].total_thumbs_up`]: !params.state ? params.key + 1 : params.key - 1
      })
      if (!params.state) {
        wx.showToast({
          title: '点赞成功',
          icon: 'none'
        })
      } else {
        wx.showToast({
          title: '取消成功',
          icon: 'none'
        })
      }
    })
  },

  xToDetail: function (e) {
    let id = e.currentTarget.dataset.id
    let openid = e.currentTarget.dataset.openid
    let totalcomment = e.currentTarget.dataset.totalcomment
    console.log(totalcomment)
    wx.navigateTo({
      url: '/subtool/pages/community/detail/index?id=' + id + '&openid=' + openid +'&totalcomment='+ totalcomment,
    })
  },

  xReady: function () {
    let that = this
    wx.cloud.callFunction({
      name: 'station_message',
      data: {
        action: "get_unread_message_count"
      }
    }).then(res => {
      console.log("返回:", res.result)
      let data = res.result
      that.setData({
        readyMsg: data
      })
    })
  },

  xToNewMessage: function () {
    wx.navigateTo({
      url: '/subtool/pages/community/msg/index',
    })
  },

  back: function () {
    wx.navigateBack({
      delta: -1,
    })
  },

  xMyLike: function () {
    let that = this
    let id = that.data.id
    let _dataObj = {
      group_id: id
    }
    if(that.data.mylike==0){
      wx.showLoading({
        title: '关注中',
      })
    }else{
      wx.showLoading({
        title: '取关中',
      })
    }
    wx.cloud.callFunction({
      name: 'dynamic_content',
      data: {
        action: "follow_group",
        dataObj: _dataObj
      }
    }).then(res => {
      console.log("返回:", res.result)
      that.setData({
        mylike : that.data.mylike ==1 ? 0 : 1
      })
      wx.hideLoading()
    })
  }
})